package com.itheima.springbootwebtlias.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.springbootwebtlias.anno.LogOperation;
import com.itheima.springbootwebtlias.mapper.StudentMapper;
import com.itheima.springbootwebtlias.pojo.ClaQueryParam;
import com.itheima.springbootwebtlias.pojo.PageResult;
import com.itheima.springbootwebtlias.pojo.StuQueryParam;
import com.itheima.springbootwebtlias.pojo.Student;
import com.itheima.springbootwebtlias.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;


    //4.1 学员列表查询,条件分页查询接口
    @Transactional(rollbackFor = Exception.class)
    @Override
    public PageResult page(StuQueryParam stuQueryParam) {
        //1.启动分页插件
       PageHelper.startPage(stuQueryParam.getPage(), stuQueryParam.getPageSize());

        //2.调用Mapper层查询分页数据
       Page<Student> pages = (Page<Student>) studentMapper.selectByCondition(stuQueryParam);

       //3.封装到PageResult对象中
        PageResult pageResult = new PageResult(pages.getTotal(), pages.getResult());
        //4.返回响应数据
        return pageResult;
    }

    //4.3 添加学员,新增学生信息接口
    @LogOperation
    @Override
    public void save(Student student) {
        //更新时间
        student.setUpdateTime(LocalDateTime.now());
        student.setCreateTime(LocalDateTime.now());
        //调用Mapper层保存数据
        studentMapper.saveStudent(student);
    }

    //4.4 根据ID查询学生 + 查询回显
    @Override
    public Student queryById(Integer id) {
        //1.调用Mapper层查询数据
        Student student = studentMapper.queryById(id);
        //2.返回封装数据
        return student;
    }

    //4.5 修改学生信息接口
    @LogOperation
    @Override
    public void update(Student student) {
        //1.调用Mapper层修改数据
        //更新时间
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);

    }

    //4.2 删除学生信息接口 + 批量删除
    @LogOperation
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeBatch(List<Integer> ids) {
        //1.调用Mapper层批量删除数据
        studentMapper.removeBatchById(ids);
    }

    //4.6 违纪处理,违纪次数和违纪扣分
    @Override
    public void violation(Integer id, Short score) {
        //1.调用Mapper层修改数据
        //更新时间
        studentMapper.violation(id,score);

    }
}
